package com.classic.base;

import android.content.Intent;
import android.net.Uri;
import android.support.annotation.DrawableRes;
import android.view.Display;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.classic.dialog.UDialog;
import com.classic.duanzhi.R;
import com.classic.utils.UtilsPrivacyPolicy;


public abstract class UBaseSplashActivity extends UBaseActivity {
    ImageView img;
    UtilsPrivacyPolicy u_utils;

    /**
     * @return 启动页显示图片
     */
    protected abstract @DrawableRes
    int getImg();

    /**
     * 允许进入请求权限
     */
    protected abstract void initPermission();

    @Override
    protected int getResourceId() {
        return R.layout.activity_splash_u;
    }

    /**
     * 只用于初始化当前app需要实例化的内容  不适用当前页面初始化数据
     */
    protected abstract void initData();

    public void initUContent() {

    }

    @Override
    protected void initView() {
        initUContent();
        img = findViewById(R.id.v_iv_img);
        u_utils = findViewById(R.id.u_utils);
        ImageView ivLogo = findViewById(R.id.iv_logo);
        Glide.with(UBaseSplashActivity.this).load(getImg()).into(img);
        ViewGroup.LayoutParams para = ivLogo.getLayoutParams();
        Display display = getWindowManager().getDefaultDisplay();
        double v = getImgLogoScale(display.getHeight());
        para.height = (int) v;
        para.width = ViewGroup.LayoutParams.MATCH_PARENT;
        Glide.with(UBaseSplashActivity.this).load(getImgLogo()).into(ivLogo);
        ivLogo.setLayoutParams(para);
        initData();
        u_utils.setOnUtilsPrivacyPolicyListener(new UtilsPrivacyPolicy.OnUtilsPrivacyPolicyListener() {
            @Override
            public void onPermission() {//允许进入 进行操作
                initPermission();
            }
        });
    }




    protected abstract String getChannel();

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    /**
     * 跳转到主页
     */
    public abstract void starMain();

    public UDialog dialog;
    public boolean isTrue = true;

    public void showDialog() {
        if (null == dialog) {
            dialog = new UDialog(UBaseSplashActivity.this, "权限提示!",
                    "打开", "退出",
                    "此应用需要打开定位、存储权限，否则部分功能将无法正常使用，是否打开？", new UDialog.ClickListener() {
                @Override
                public void doConfirm() {
                    Intent intent = new Intent();
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                    intent.setData(Uri.fromParts("package", getPackageName(), null));
                    startActivity(intent);
                    dialog.dismiss();
                    isTrue = true;
                }

                @Override
                public void doCancel() {
                    finish();
                }

                @Override
                public void doDismiss() {
                    isTrue = true;
                    initPermission();
                }
            });
        }
        dialog.show();
    }

    /**
     * @return 启动页显示图片
     */
    protected abstract @DrawableRes
    int getImgLogo();


    /**
     * @param height
     * @return 动态计算logo高度 适配屏幕
     */
    public abstract double getImgLogoScale(int height);
}
